package cn.school.mybatis.servlet;

import cn.school.mybatis.pojo.Grade;
import cn.school.mybatis.pojo.Student;
import cn.school.mybatis.service.GradeService;
import cn.school.mybatis.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/toUpdateStudentServlet")
public class toUpdateStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();

        int num = new Integer(request.getParameter("stuno"));
        Student student = StudentService.STUDENT_SERVICE.get(num);
        if (student == null) {
            out.print("<script>alert('未找到相关信息！');history.back();</script>");
            return;
        }
        out.print(student);
        //年级名称
        List<Grade> lists = GradeService.GRADE_SERVICE.searchAll();
//        String gradeName=lists.get(student.getGradeid()).getGname();

        request.setAttribute("student",student);
//        request.setAttribute("gradeName",gradeName);
        request.setAttribute("gradeLists",lists);

        request.getRequestDispatcher("/AdminLTEdemo01/s_student/s_studentUpdate.jsp").forward(request,response);
//        request.getRequestDispatcher(request.getContextPath()+"/AdminLTEdemo01/s_student/s_studentUpdate.jsp").forward(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
